package com.nariano.server.utils.copy;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class CopyUtil
{
        public static Object deepCopy(Object orig)
        {
                if (orig == null)
                {
                        return null;
                }
                Object obj = null;
                try
                {
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        ObjectOutputStream out = new ObjectOutputStream(bos);
                        out.writeObject(orig);
                        out.flush();
                        out.close();

                        ObjectInputStream in = new ObjectInputStream(
                                        new ByteArrayInputStream(bos.toByteArray()));
                        obj = in.readObject();
                }
                catch(IOException e)
                {
                        throw new RuntimeException("Failed to copy object " + e.getMessage(), e);
                }
                catch(ClassNotFoundException cnfe)
                {
                        throw new RuntimeException("Failed to copy object " + cnfe.getMessage(), cnfe);
                }
                return obj;
        }
}
